package com.ncst.backend.controller;

import com.ncst.backend.common.Result;
//import com.ncst.backend.domain.dto.RefundReqDto;
import com.ncst.backend.domain.vo.RefundDrugVo;
import com.ncst.backend.domain.vo.RefundPatientVo;
import com.ncst.backend.service.RefundService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/refund")
@RequiredArgsConstructor
public class RefundController {

    private final RefundService refundService;

    /** 根据病历号或姓名搜患者（模糊） */
    @GetMapping("/patient")
    public Result<List<RefundPatientVo>> searchPatient(
            @RequestParam(required = false) String caseNumber,
            @RequestParam(required = false) String realName) {
        return Result.success(refundService.searchPatient(caseNumber, realName));
    }

    /** 该患者“已发药”药品列表 */
    @GetMapping("/drugs/{caseNumber}")
    public Result<List<RefundDrugVo>> listRefundableDrugs(@PathVariable String caseNumber,
                                                          @RequestParam(required = false) String realName) {
        return Result.success(refundService.listRefundableDrugs(caseNumber, realName));
    }

    /** 退药：把“已发药”→“已缴费” */
    @PostMapping
    public Result<Integer> refund(@RequestBody List<Integer> prescriptionIds) {
        if (prescriptionIds.isEmpty()) return Result.badRequest("未选择药品");
        int rows = refundService.refund(prescriptionIds);
        return rows == 0 ? Result.error("退药失败") : Result.success("退药成功", rows);
    }
}